package com.ruoyi.media.mediaPlatform.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.media.mediaPlatform.domain.MediaPlatform;
import com.ruoyi.media.mediaPlatform.domain.bo.MediaPlatformBo;
import com.ruoyi.media.mediaPlatform.domain.vo.MediaPlatformVo;
import com.ruoyi.media.mediaPlatform.mapper.MediaPlatformMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.media.mediaPlatform.service.IMediaPlatformService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 自媒体平台配置Service业务层处理
 *
 * @author dqw
 * @date 2023-03-23
 */
@RequiredArgsConstructor
@Service
public class MediaPlatformServiceImpl implements IMediaPlatformService {

    private final MediaPlatformMapper baseMapper;

    /**
     * 查询自媒体平台配置
     */
    @Override
    public MediaPlatformVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询自媒体平台配置列表
     */
    @Override
    public TableDataInfo<MediaPlatformVo> queryPageList(MediaPlatformBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<MediaPlatform> lqw = buildQueryWrapper(bo);
        Page<MediaPlatformVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询自媒体平台配置列表
     */
    @Override
    public List<MediaPlatformVo> queryList(MediaPlatformBo bo) {
        LambdaQueryWrapper<MediaPlatform> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<MediaPlatform> buildQueryWrapper(MediaPlatformBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<MediaPlatform> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getDeptId() != null, MediaPlatform::getDeptId, bo.getDeptId());
        lqw.eq(bo.getUserId() != null, MediaPlatform::getUserId, bo.getUserId());
        lqw.like(StringUtils.isNotBlank(bo.getPlatformName()), MediaPlatform::getPlatformName, bo.getPlatformName());
        lqw.eq(StringUtils.isNotBlank(bo.getUrl()), MediaPlatform::getUrl, bo.getUrl());
        lqw.eq(StringUtils.isNotBlank(bo.getPlatformCode()), MediaPlatform::getPlatformCode, bo.getPlatformCode());
        lqw.eq(bo.getStatus() != null, MediaPlatform::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增自媒体平台配置
     */
    @Override
    public Boolean insertByBo(MediaPlatformBo bo) {
        MediaPlatform add = BeanUtil.toBean(bo, MediaPlatform.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改自媒体平台配置
     */
    @Override
    public Boolean updateByBo(MediaPlatformBo bo) {
        MediaPlatform update = BeanUtil.toBean(bo, MediaPlatform.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(MediaPlatform entity){
        //TODO 做一些数据校验,如唯一约束
        LoginUser loginUser =  LoginHelper.getLoginUser();
        entity.setUserId(loginUser.getUserId());
        entity.setDeptId(loginUser.getDeptId());
    }

    /**
     * 批量删除自媒体平台配置
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }



    /**
     * 修改当前状态
     * @return
     */
    @Override
    public int updatePlatformStatus(MediaPlatformBo bo) {
        MediaPlatform mediaPlatform = BeanUtil.toBean(bo, MediaPlatform.class);
        return baseMapper.updateById(mediaPlatform);
    }
}
